package site.potatoblog.threadpool.manager.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@ConditionalOnProperty(prefix = "spring.mail",name = {"host","protocol","username","password","port"})
@Component
@Async
public class Alert {
    private Long lastActiveAlert;
    private Long lastCapacityAlert;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    TemplateEngine templateEngine;
    public synchronized void alertActive(DynamicThreadPool threadPool){
        if(lastActiveAlert==null||System.currentTimeMillis()-lastActiveAlert>=300000){
            lastActiveAlert=System.currentTimeMillis();
            MimeMessage msg=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(msg);
            try {
                helper.setTo(threadPool.getAlerEmail());
                helper.setFrom(mailProperties.getUsername());
                helper.setSubject("线程池告警");
                helper.setSentDate(new Date());
                Context context = new Context();
                context.setVariable("isActive",true);
                context.setVariable("active",(double)threadPool.getActiveCount()/threadPool.getMaxPoolSize());
                context.setVariable("activeThreshold", threadPool.getActiveThreshold());
                context.setVariable("rejectCount", threadPool.getRejectCount());
                context.setVariable("threadPoolName", threadPool.getThreadPoolName());
                context.setVariable("corePoolSize", threadPool.getCorePoolSize());
                context.setVariable("maxPoolSize", threadPool.getMaxPoolSize());
                context.setVariable("poolSize", threadPool.getPoolSize());
                context.setVariable("activeCount", threadPool.getActiveCount());
                context.setVariable("queueType", threadPool.getQueue().getClass().getSimpleName());
                context.setVariable("queueCapacity", ((AdaptBlockingQueue<Runnable>)threadPool.getQueue()).getCapacity());
                context.setVariable("queueSize", threadPool.getQueue().size());
                context.setVariable("queueRemainingCapacity", threadPool.getQueue().remainingCapacity());
                context.setVariable("completedTaskCount", threadPool.getCompletedTaskCount());
                context.setVariable("largestPoolSize", threadPool.getLargestPoolSize());
                String mail=templateEngine.process("mail",context);
                helper.setText(mail,true);
                javaMailSender.send(msg);

            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void alertCapacity(DynamicThreadPool threadPool){
        if(lastCapacityAlert==null||System.currentTimeMillis()-lastCapacityAlert>=300000){
            lastCapacityAlert=System.currentTimeMillis();
            MimeMessage msg=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(msg);
            try {
                helper.setTo(threadPool.getAlerEmail());
                helper.setFrom(mailProperties.getUsername());
                helper.setSubject("线程池告警");
                helper.setSentDate(new Date());
                Context context = new Context();
                context.setVariable("isActive",false);
                context.setVariable("capacityPer",(double)threadPool.getQueue().size()/threadPool.getQueueCapacity());
                context.setVariable("capacityThreshold",threadPool.getCapacityThreshold());
                context.setVariable("activeThreshold", threadPool.getActiveThreshold());
                context.setVariable("rejectCount", threadPool.getRejectCount());
                context.setVariable("threadPoolName", threadPool.getThreadPoolName());
                context.setVariable("corePoolSize", threadPool.getCorePoolSize());
                context.setVariable("maxPoolSize", threadPool.getMaxPoolSize());
                context.setVariable("poolSize", threadPool.getPoolSize());
                context.setVariable("activeCount", threadPool.getActiveCount());
                context.setVariable("queueType", threadPool.getQueue().getClass().getSimpleName());
                context.setVariable("queueCapacity", ((AdaptBlockingQueue<Runnable>)threadPool.getQueue()).getCapacity());
                context.setVariable("queueSize", threadPool.getQueue().size());
                context.setVariable("queueRemainingCapacity", threadPool.getQueue().remainingCapacity());
                context.setVariable("completedTaskCount", threadPool.getCompletedTaskCount());
                context.setVariable("largestPoolSize", threadPool.getLargestPoolSize());
                String mail=templateEngine.process("mail",context);
                helper.setText(mail,true);
                javaMailSender.send(msg);

            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }

}
